package com.example.opengl.render.circle

import android.opengl.GLES20
import android.opengl.GLES20.*
import com.android.baselibrary.app.MainApplication
import com.example.opengl.R
import com.example.opengl.base.TextResourceReader
import com.example.opengl.render.base.IDrawer
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import java.util.*
import kotlin.math.cos
import kotlin.math.sin

/**
 * @author: Mr.You
 * @create: 2023-07-27 11:10
 * @description: 绘制圆形
 **/
class CircleDrawer : IDrawer {
    var mScreenWidth = 0
    var mScreenHeight = 0
    var mProgram = -1
    var mVertexCoords = floatArrayOf(
        0.5f, 0.5f,
        -0.5f, 0.5f,
        -0.5f, -0.5f,
        0.5f, -0.5f
    )
    var mTextureCoords = floatArrayOf(
        0.0f, 1.0f, 0.0f, 1.0f,
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f
    )
    var mVertexFloatBuffer: FloatBuffer? = null
    var mTextureFloatBuffer: FloatBuffer? = null
    var mVertexShader = -1
    var mTextureShader = -1
    var mTextureId = -1


    override fun draw() {
        if (mTextureId != -1) {
            initShader()
            doDraw()
        }

    }


    private fun initShader() {
        mVertexShader = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(
            mVertexShader,
            TextResourceReader.readTextFileFormResource(resourceId = R.raw.vertex_color_triangle_shader)
        )

        mTextureShader = glCreateShader(GL_FRAGMENT_SHADER)
        glShaderSource(
            mTextureShader,
            TextResourceReader.readTextFileFormResource(resourceId = R.raw.fragment_color_triangle_shader)
        )

        glCompileShader(mVertexShader)
        glCompileShader(mTextureShader)

        mProgram = glCreateProgram()

        glAttachShader(mProgram, mVertexShader)
        glAttachShader(mProgram, mTextureShader)

        glLinkProgram(mProgram)
        glUseProgram(mProgram)

        glDeleteShader(mTextureShader)
        glDeleteShader(mVertexShader)

    }

    private fun doDraw() {

        glEnableVertexAttribArray(0)
        glEnableVertexAttribArray(1)

        glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, mVertexFloatBuffer)
        glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, mTextureFloatBuffer)

        glDrawArrays(GL_TRIANGLE_FAN, 0, 60)

    }


    override fun setTextureID(id: Int) {
        mTextureId = id
    }

    override fun release() {

    }

    override fun setWorldSize(worldW: Int, worldH: Int) {
        mScreenWidth = worldW
        mScreenHeight = worldH
        initCoords()

    }

    private fun initCoords() {
        //计算绘制圆形的坐标
        val radius = 0.5f
        val centerX = 0f
        val centerY = 0f
        //定义需要多少个坐标点确定一个圆
        val num = 60

        val line = 2 * Math.PI / num.toFloat()


        val target = FloatArray(num*3)


        for (i in 0 until num) {
            target[i * 3] = (centerX + radius * cos(line * i)).toFloat()
            target[i * 3 + 1] = (centerY + radius * sin(line * i)).toFloat()
          //  target[i * 3 + 2] = 0f
        }


        //通过宽高比，将y轴转化为和x轴长度保持一致。
        val ration = mScreenWidth / mScreenHeight.toFloat()
        val floats = Arrays.copyOf(target, target.size)
        val limit=  floats.size/3
        for (i in 0 until limit){
            floats[1+i *3]  *=ration
        }


        val bv = ByteBuffer.allocateDirect(floats.size * 4)
        bv.order(ByteOrder.nativeOrder())
        mVertexFloatBuffer = bv.asFloatBuffer()
        mVertexFloatBuffer?.run {
            put(floats)
            position(0)
        }

        val bt = ByteBuffer.allocateDirect(mTextureCoords.size * 4)
        bt.order(ByteOrder.nativeOrder())
        mTextureFloatBuffer = bt.asFloatBuffer()
        mTextureFloatBuffer?.run {
            put(mTextureCoords)
            position(0)
        }
    }

    override fun setVideoSize(videoW: Int, videoH: Int) {

    }
}